﻿using AutoMapper;
using BW.MesProject.BasicData.Domain.基础;
using BW.MesProject.BasicData.Infrastructure;
using BW.MesProject.Public.ErrorCode;
using BW.MesProject.Public.Infrastructure;
using Yitter.IdGenerator;

namespace BW.MesProject.BasicData.Application
{
    /// <summary>
    /// 车间服务实现类
    /// 提供车间的增删改查等业务功能
    /// </summary>
    public class WorkShopServices : IWorkShopServices
    {
        private readonly IBaseRepository<Workshopmanagementmodel> shop;
        private readonly IMapper mapper;

        /// <summary>
        /// 构造函数，通过依赖注入获取仓储和映射器
        /// </summary>
        /// <param name="shop">车间仓储接口</param>
        /// <param name="mapper">AutoMapper映射器</param>
        public WorkShopServices(IBaseRepository<Workshopmanagementmodel> shop, IMapper mapper)
        {
            this.shop = shop;
            this.mapper = mapper;
        }

        /// <summary>
        /// 添加车间
        /// </summary>
        /// <param name="model">车间模型</param>
        /// <returns>添加结果</returns>
        public ApiResult<int> AddWorkShop(Workshopmanagementmodel model)
        {
            //添加
            var models = mapper.Map<Workshopmanagementmodel>(model);
            model.Id = YitIdHelper.NextId();
            model.IsDeleted = false;
            return new ApiResult<int>()
            {
                Code = ApiEnums.Success,
                Message = "添加成功",
                Data = shop.Add(models),
            };
        }

        /// <summary>
        /// 删除车间（逻辑删除）
        /// </summary>
        /// <param name="id">车间ID</param>
        /// <returns>删除结果</returns>
        public ApiResult<int> DeleteWorkShop(long id)
        {
            //根据id修改逻辑删除为 true
            var model = shop.GetValue(x => x.Id == id);
            model.IsDeleted = true;
            return new ApiResult<int>()
            {
                Code = ApiEnums.Success,
                Message = "删除成功",
                Data = shop.Update(model),
            };

        }

        /// <summary>
        /// 获取所有未删除的车间
        /// </summary>
        /// <returns>车间列表</returns>
        public ApiResult<List<Workshopmanagementmodel>> GetAllShop()
        {
            return new ApiResult<List<Workshopmanagementmodel>>()
            {
                Code = ApiEnums.Success,
                Message = "查询成功",
                Data = shop.GetAll().Where(x => x.IsDeleted == false).ToList(),
            };
        }

        /// <summary>
        /// 根据ID获取车间
        /// </summary>
        /// <param name="id">车间ID</param>
        /// <returns>车间信息</returns>
        public ApiResult<List<Workshopmanagementmodel>> GetShop(long id)
        {

            return new ApiResult<List<Workshopmanagementmodel>>()
            {
                Code = ApiEnums.Success,
                Message = "查询成功",
                Data = shop.GetAll().Where(x => x.Id == id).ToList(),
            };
        }

        /// <summary>
        /// 分页查询车间
        /// </summary>
        /// <param name="shopcode">车间编码（可选）</param>
        /// <param name="shopname">车间名称（可选）</param>
        /// <param name="pageindex">页码</param>
        /// <param name="pagesize">每页大小</param>
        /// <returns>分页结果</returns>
        public ApiPaging<List<Workshopmanagementmodel>> GetWorkShop(string? shopcode, string? shopname, int pageindex, int pagesize)
        {
            //查询名称来显示数据
            var list = shop.GetAll().Where(x => x.IsDeleted == false);
            if (shopname != null)
            {
                list = list.Where(x => x.Workshopname.Contains(shopname));
            }
            if (shopcode != null)
            {
                list = list.Where(x => x.Workshopcode.Contains(shopcode));
            }

            //分页
            list = list.Skip((pageindex - 1) * pagesize).Take(pagesize);
            return new ApiPaging<List<Workshopmanagementmodel>>()
            {
                Code = ApiEnums.Success,
                Message = "查询成功",
                Data = list.ToList(),
                TotalCount = list.Count(),
            };
        }

        /// <summary>
        /// 更新车间信息
        /// </summary>
        /// <param name="model">车间模型</param>
        /// <returns>更新结果</returns>
        public ApiResult<int> UpdateWorkShop(Workshopmanagementmodel model)
        {
            //根据id查询是否有这条数据没有就修改不了
            var query = shop.GetValue(x => x.Id == model.Id);
            if (query == null)
            {
                return new ApiResult<int>()
                {
                    Code = ApiEnums.Fail,
                    Message = "修改失败"
                    ,
                    Data = 0
                };
            }
            //保存添加人和添加时间，添加修改人和修改时间
            model.UpdatedBy = "admin";
            model.UpdatedTime = DateTime.Now;
            shop.Update(model);
            return new ApiResult<int>()
            {
                Code = ApiEnums.Success,
                Message = "修改成功",
                Data = 1
            };
            //使用修改方法修改成功

        }
    }
}
